
var rootDocument = 'http://20d6z36665.51mypc.cn'; //   'https://wx.mamatx.com.cn';//  //'https://www.momjoin.com.cn'; // 

var util = require('../utils/util.js');
//get请求
function getReq(url,data,callBack,showloading,title){
  
  //是否展示提示框
  if (showloading){
    let loadingMsg = '加载中';
    util.showLoading(title || loadingMsg );
  }
  wx.request({
    url: rootDocument + url,
    data: data,
    header: {
      'content-type': 'application/json'
    },
    method: 'GET',
    dataType: 'json',
    responseType: 'text',
    success: function(res) {
      //返回callBack 函数
      return typeof callBack == 'function' && callBack(res.data);
    },
    fail: function(res) {
      wx.showModal({
        title: '网络错误',
        content: '网络出错，请刷新重试',
        showCancel: false,
      })
      return typeof callBack == 'function' && callBack(false);
    },
    complete:function(){
      if (showloading){
        wx.hideLoading();
      }
    }
  })
}


//post请求
function postReq(url, data, callBack, showloading, title){
  //是否展示提示框
  if (showloading) {
    let loadingMsg = '加载中';
    util.showLoading(title || loadingMsg);
  }
  wx.request({
    url: rootDocument + url,
    data: data,
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    method: 'POST',
    dataType: 'json',
    responseType: 'text',
    success: function (res) {
      wx.hideLoading();
      //返回callBack 函数
      return typeof callBack == 'function' && callBack(res.data);
    },
    fail: function (res) {
      wx.hideLoading();

      wx.showModal({
        title: '网络错误',
        content: '网络出错，请刷新重试',
        showCancel: false,
      })

      return typeof callBack == 'function' && callBack(false);
    },
    complete: function () {
      if (showloading) {
        wx.hideLoading();
      }
    }
  })
}

module.exports = {
  getReq: getReq,
  postReq: postReq,
  rootDocument: rootDocument
}